<template>
  <a-modal
    :visible="visible"
    :title="t('edit')"
    :cancelText="t('cancel')"
    :okText="t('submit')"
    @ok="submit"
    @cancel="cancel(false)"
  >
    <a-form
       ref="formRef"
      :model="record"
      :rules="formRules"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
    >
    
      <a-form-item :label="t('higher')">
        <a-tree-select
          v-model:value="record.parent"
          style="width: 100%;"
          :dropdown-style="{ maxHeight: '360px', overflow: 'auto' }"
          :tree-data="state.permissions"
          :placeholder="t('higher permission')"
          replace
          tree-default-expand-all
          :fieldNames="fieldNames" 
        >
        </a-tree-select>
      </a-form-item>
      <a-form-item ref="title" :label="t('title')" name="title">
        <a-input v-model:value="record.title" />
      </a-form-item> 
      <a-form-item  ref="path" :label="t('path')" name="path">
        <a-input v-model:value="record.path" />
      </a-form-item>
      <a-form-item  ref="name" :label="t('name')" name="name">
        <a-input v-model:value="record.name" />
      </a-form-item>
      <a-form-item  :label="t('icon')"  ref="icon" >
        <go2-icon-picker v-model:value="record.icon"></go2-icon-picker>
      </a-form-item> 
      <a-form-item ref="type" :label="t('type')" name="type">
        <a-select v-model:value="record.type">
          <a-select-option value="dir"> 目录 </a-select-option>
          <a-select-option value="menu"> 菜单 </a-select-option>
          <a-select-option value="button"> 按钮 </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item ref="sort" :label="t('sort')" name="sort">
        <a-input-number v-model:value="record.sort" />
      </a-form-item>
      <a-form-item :label="t('status')" name="status">
        <a-switch :checkedValue="1" :unCheckedValue="2" :checkedChildren="t('enable')" :unCheckedChildren="t('disable')" v-model:checked="record.status" />
     
      </a-form-item>

      <a-form-item :label="t('show in menu')" name="isshowmenu">
        <a-switch :checkedValue="1" :unCheckedValue="2" :checkedChildren="t('enable')" :unCheckedChildren="t('disable')" v-model:checked="record.isshowmenu" />
     
      </a-form-item>

      <a-form-item :label="t('is record')" name="isrecord">
        <a-switch :checkedValue="1" :unCheckedValue="2" :checkedChildren="t('enable')" :unCheckedChildren="t('disable')" v-model:checked="record.isrecord" />
     
      </a-form-item>
      <!-- <a-form-item label="备注" name="remark">
        <a-textarea v-model:value="record.remark" />
      </a-form-item> -->
    </a-form>
  </a-modal>
</template>
<script>
import { useI18n  } from "vue-i18n";
import { message } from 'ant-design-vue';
import { edit, tree } from "@/core/api/module/permission";
import { defineComponent, reactive, ref, toRaw, watch } from "vue";
export default defineComponent({
  props: {
    visible: {
      type: Boolean,
    },
    record: {
      type: Object
    }
  },
  emit: ["close"],
  setup(props, context) {
    const { t } = useI18n();
    const state = reactive({
      depts: [],
      showComponent: true,
      showPath: true,
      showCode: false
    });
    
    const formRef = ref();
    
    let formState = reactive({ 
    });

  

    const formRules = {
      title: [{ required: true, message: t('Please input a permission name'), trigger: "blur" }],//"请输入权限名称" 
      type: [{ required: true, message: t('Please input permission type'), trigger: "blur" }],
      parent: [{ required: true, message: t('Please select superior permission'), trigger: "change" }],
    };
 

    const submit = (e) => {
      formRef.value
        .validate()
        .then(() => {
          edit(toRaw(props.record)).then((response)=>{
              if(response.code== 0 ){
                message.success({ content: t('Saved successfully!'), duration: 1 }).then(()=>{
                  cancel(true);
                });
              }else{
                message.error({ content: response.msg, duration: 1 }).then(()=>{
                  cancel(false);
                });
              }
          });
        })
        .catch(error => {
          console.log('error', error);
        });
    };

     
    const cancel = (e=false) => {
      formRef.value.resetFields();
      context.emit("close", e);
    };

    const loadpermission = () => {
      tree({}).then((response)=>{
        response.data = [{
          id:0,
          title: t("top menu"),
          children: response.data
        }]
        state.permissions = response.data;
      })
    }

    loadpermission();

    return {
      t,
      state,
      submit, 
      cancel,
      formRef,
      formState,
      formRules,
      
      labelCol: { span: 6 },
      wrapperCol: { span: 18 },

      fieldNames: {children:'children', title:'title', key:'id', value: 'id' }
    };
  },
});
</script>